//
//  AreasPickerView.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/10/31.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "AreasPickerView.h"

@implementation AreasPickerView

-(instancetype)initWithFrame:(CGRect)frame
{
//    self = [super initWithFrame:frame];

    self = [[[NSBundle mainBundle] loadNibNamed:@"AreasPickerView" owner:self options:nil] lastObject];
    self.frame = frame;
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.pickerView.dataSource = self;
        self.pickerView.delegate = self;
        self.provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"region" ofType:@"plist"]];
        self.pickerView.backgroundColor = [UIColor whiteColor];

        self.provinceStr = [[self.provinces objectAtIndex:0] objectForKey:@"region_name"];
        self.region_id = [[self.provinces objectAtIndex:0] objectForKey:@"region_id"];
        
        self.cities = [[self.provinces objectAtIndex:0] objectForKey:@"sub"];
        if (self.cities.count > 0) {
            self.cityStr = [[self.cities objectAtIndex:0] objectForKey:@"region_name"];
            self.region_id = [[self.cities objectAtIndex:0] objectForKey:@"region_id"];
            self.areas = [[self.cities objectAtIndex:0] objectForKey:@"sub"];
            if (self.areas.count > 0) {
                self.distictStr = [[self.areas objectAtIndex:0] strForKey:@"region_name"];
                self.region_id = [[self.areas objectAtIndex:0] strForKey:@"region_id"];
            } else {
                self.distictStr = @"";
            }
        } else {
            self.cityStr = @"";
        }
    }
    return self;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [self.provinces count];
            break;
        case 1:
            return [self.cities count];
            break;
        case 2:
            return [self.areas count];
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        switch (component) {
            case 0:
                return [[self.provinces objectAtIndex:row] objectForKey:@"region_name"];
                break;
            case 1:
                if ([self.cities count] > 0) {
                    return [[self.cities objectAtIndex:row] objectForKey:@"region_name"];
                    break;
                }
            case 2:
                if ([self.areas count] > 0) {
                    return [[self.areas objectAtIndex:row] strForKey:@"region_name"];
                    break;
                }
            default:
                return  @"";
                break;
        }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
        switch (component) {
            case 0:
                self.cities = [[self.provinces objectAtIndex:row] objectForKey:@"sub"];
                [self.pickerView selectRow:0 inComponent:1 animated:YES];
                [self.pickerView reloadComponent:1];
                
                if ([self.cities count] > 0) {
                    self.areas = [[self.cities objectAtIndex:0] objectForKey:@"sub"];
                } else {
                    self.areas = [NSArray array];
                }
                [self.pickerView selectRow:0 inComponent:2 animated:YES];
                [self.pickerView reloadComponent:2];
                
                self.provinceStr = [[self.provinces objectAtIndex:row] objectForKey:@"region_name"];
                self.region_id = [[self.provinces objectAtIndex:row] objectForKey:@"region_id"];
                
                if ([self.cities count] > 0) {
                    self.cityStr = [[self.cities objectAtIndex:0] objectForKey:@"region_name"];
                    self.region_id = [[self.cities objectAtIndex:0] objectForKey:@"region_id"];
                    if ([self.areas count] > 0) {
                        self.distictStr = [[self.areas objectAtIndex:0] strForKey:@"region_name"];
                        self.region_id = [[self.areas objectAtIndex:0] strForKey:@"region_id"];
                    } else{
                        self.distictStr = @"";
                    }
                } else {
                    self.cityStr = @"";
                    self.distictStr = @"";
                }
//                self.provinceLabel.text = self.provinceStr;
                
                break;
            case 1:
                if ([self.cities count] > 0) {
                    self.areas = [[self.cities objectAtIndex:row] objectForKey:@"sub"];
                } else {
                    [self.pickerView selectRow:0 inComponent:1 animated:YES];
                    [self.pickerView reloadComponent:1];
                }
                
                [self.pickerView selectRow:0 inComponent:2 animated:YES];
                [self.pickerView reloadComponent:2];
                
                if ([self.cities count] > 0) {
                    self.cityStr = [[self.cities objectAtIndex:row] objectForKey:@"region_name"];
                    self.region_id = [[self.cities objectAtIndex:row] objectForKey:@"region_id"];
                    if ([self.areas count] > 0) {
                        self.distictStr = [[self.areas objectAtIndex:0] strForKey:@"region_name"];
                        self.region_id = [[self.areas objectAtIndex:0] strForKey:@"region_id"];
                    } else{
                        self.distictStr = @"";
                    }
                } else {
                    self.cityStr = @"";
                }
//                self.cityLabel.text = self.cityStr;
                break;
            case 2:
                if ([self.areas count] > 0) {
                    self.distictStr = [[self.areas objectAtIndex:row] strForKey:@"region_name"];
                    self.region_id = [[self.areas objectAtIndex:row] strForKey:@"region_id"];
                } else{
                    self.distictStr = @"";
                }
//                self.areaLabel.text = self.distictStr;
                break;
            default:
                break;
        }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel *)view;
    
    if (!label){
        label = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        label.adjustsFontSizeToFitWidth = YES;
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [label setFont:[UIFont boldSystemFontOfSize:16]];
    }
    // Fill the label text here
    label.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    
    return label;
}

- (IBAction)doneBtn:(id)sender {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(getArea:)]) {
        [self.delegate getArea:[NSString stringWithFormat:@"%@%@%@",self.provinceStr,self.cityStr,self.distictStr]];
    }
    [sender removeFromSuperview];
    [self removeFromSuperview];

}


@end
